package com.liug.droidsmall2.utils.view;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Created by 刘国 on 2018/7/30.
 * 底部tab封装类
 */

public class BootomTabUtils {

    private String LOG = "BootomTabUtils_Log";
    private LinearLayout bottom_tab;
    View frame;
    private RelativeLayout linearLayoutAll;

    public View editBootomView(int lyout_ID, Activity context, int[] selectedIcon, int[] disSelectedIcon, String[] text_bottom, int defaultIndex) {

        View view = View.inflate(context, lyout_ID, null);


        this.frame = frame;
        LinearLayout linearLayoutre = null;
        if ((selectedIcon.length == 5) && (disSelectedIcon.length == 5) && (text_bottom.length == 5)) {

            bottom_tab = new LinearLayout(context);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            bottom_tab.setOrientation(LinearLayout.VERTICAL);
            bottom_tab.setLayoutParams(layoutParams);

            LinearLayout linear_s = new LinearLayout(context);
            LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            linear_s.setLayoutParams(layoutParams1);
            linear_s.setOrientation(LinearLayout.HORIZONTAL);

            bottom_tab.setBackgroundColor(Color.parseColor("#ffffff"));

            for (int i = 0; i < selectedIcon.length; i++) {
                LinearLayout ll = new LinearLayout(context);
                LinearLayout.LayoutParams ll_layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
                ll_layoutParams.weight = 1f;
                ll_layoutParams.setMargins(2, 8, 2, 2);
                ll.setOrientation(LinearLayout.VERTICAL);
                ll.setLayoutParams(ll_layoutParams);
                ImageView imageView = new ImageView(context);
                TextView textView = new TextView(context);
                textView.setText(text_bottom[i]);
                ll.addView(imageView);
                ll.addView(textView, ll_layoutParams);
                textView.setGravity(Gravity.CENTER);

                if (i == defaultIndex) {
                    imageView.setImageResource(selectedIcon[i]);
                    textView.setTextColor(Color.parseColor("#FE671E"));
                } else {
                    imageView.setImageResource(disSelectedIcon[i]);
                }
                linear_s.addView(ll, i);
            }

            View view1 = new View(context);
            LinearLayout.LayoutParams view_line = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 2);
            view1.setLayoutParams(view_line);
            view1.setBackgroundColor(Color.parseColor("#FE671E"));
            bottom_tab.addView(view1);
            bottom_tab.addView(linear_s);


           addBootomView(view, context);


            addClickListener(bottom_tab, text_bottom, selectedIcon, disSelectedIcon, context);

        } else {

            Log.e(LOG, "ERROR:您editBootomView方法入参的数组长度不相等");
            return linearLayoutre;
        }

        return linearLayoutre;
    }

    /**
     * 做点击ui改变
     *
     * @param index
     * @param selectedIcon
     * @param disSelectedIcon
     */
    private void doforView(int index, int[] selectedIcon, int[] disSelectedIcon) {


        View childAt = bottom_tab.getChildAt(1);
        LinearLayout childAt1 = (LinearLayout) childAt;
        int childCount = childAt1.getChildCount();

        for (int i = 0; i < childCount; i++) {
            LinearLayout ll = (LinearLayout) childAt1.getChildAt(i);
            if (index == i) {
                ImageView imageView = (ImageView) ll.getChildAt(0);
                TextView textView = (TextView) ll.getChildAt(1);
                imageView.setImageResource(selectedIcon[index]);
                textView.setTextColor(Color.parseColor("#FE671E"));
            } else {
                ImageView imageView = (ImageView) ll.getChildAt(0);
                TextView textView = (TextView) ll.getChildAt(1);
                imageView.setImageResource(disSelectedIcon[i]);
                textView.setTextColor(Color.parseColor("#8A000000"));

            }
        }
    }


    /**
     * 添加监听
     *
     * @param bottom_tab
     * @param text_bottom
     * @param selectedIcon
     * @param disSelectedIcon
     * @param context
     */
    private void addClickListener(final LinearLayout bottom_tab, final String[] text_bottom, final int[] selectedIcon, final int[] disSelectedIcon, final Activity context) {

        LinearLayout childAt1 = (LinearLayout) bottom_tab.getChildAt(1);

        int childCount = childAt1.getChildCount();
        for (int i = 0; i < childCount; i++) {

            final View childAt = childAt1.getChildAt(i);
            final int finalI = i;

            childAt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    anmin(finalI);
                    switch (finalI) {
                        case 0:
                            doforView(0, selectedIcon, disSelectedIcon);
                            listener.clickTab1(view, finalI, text_bottom[finalI]);
                            break;
                        case 1:
                            doforView(1, selectedIcon, disSelectedIcon);
                            listener.clickTab2(view, finalI, text_bottom[finalI]);
                            break;
                        case 2:
                            doforView(2, selectedIcon, disSelectedIcon);
                            listener.clickTab3(view, finalI, text_bottom[finalI]);
                            break;
                        case 3:
                            doforView(3, selectedIcon, disSelectedIcon);
                            listener.clickTab4(view, finalI, text_bottom[finalI]);
                            break;
                        case 4:
                            doforView(4, selectedIcon, disSelectedIcon);
                            listener.clickTab5(view, finalI, text_bottom[finalI]);
                            break;
                    }
                }
            });
        }
    }


    /**
     * 动画
     *
     * @param finalI
     */
    public void anmin(int finalI) {
        LinearLayout childAt = (LinearLayout) bottom_tab.getChildAt(1);
        LinearLayout ll = (LinearLayout) childAt.getChildAt(finalI);
        ImageView imageView = (ImageView) ll.getChildAt(0);
        ObjectAnimator a1 = ObjectAnimator.ofFloat(imageView, "rotation", 0F, 360F);
        a1.setInterpolator(new BounceInterpolator());
        AnimatorSet set = new AnimatorSet();
        set.setDuration(1000);
        set.playTogether(a1);
        set.start();
    }

    /**
     * 添加底部tab
     */
    public LinearLayout addBootomView(View view, Activity context) {
        LinearLayout linearLayoutss = null;
        if (bottom_tab != null) {

            //外层
            linearLayoutss = new LinearLayout(context);
            linearLayoutss.setOrientation(LinearLayout.VERTICAL);
            LinearLayout.LayoutParams topContentTextView_lp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);//此处我需要均分高度就在heignt处设0,1.0f即设置权重是1，页面还有其他一个控件,1：1高度就均分了
            linearLayoutss.setLayoutParams(topContentTextView_lp1);

            //frame包裹
            LinearLayout linearLayout1 = new LinearLayout(context);
            linearLayout1.setOrientation(LinearLayout.VERTICAL);
            linearLayout1.setGravity(Gravity.BOTTOM);
            linearLayout1.addView(bottom_tab);

            //底部栏包裹
            if (view != null) {
                LinearLayout linearLayout2 = new LinearLayout(context);
                linearLayout2.setOrientation(LinearLayout.VERTICAL);
                LinearLayout.LayoutParams topContentTextView_lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.0f);//此处我需要均分高度就在heignt处设0,1.0f即设置权重是1，页面还有其他一个控件,1：1高度就均分了
                linearLayout2.setLayoutParams(topContentTextView_lp);
                linearLayout2.addView(view);
                linearLayoutss.addView(linearLayout2);
            }else{

                LinearLayout linearLayout2 = new LinearLayout(context);
                linearLayout2.setOrientation(LinearLayout.VERTICAL);
                LinearLayout.LayoutParams topContentTextView_lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.0f);//此处我需要均分高度就在heignt处设0,1.0f即设置权重是1，页面还有其他一个控件,1：1高度就均分了
                linearLayout2.setLayoutParams(topContentTextView_lp);
                linearLayout2.addView(view);
                linearLayoutss.addView(linearLayout2);

            }
            linearLayoutss.addView(linearLayout1);

            context.setContentView(linearLayoutss);
        } else {

            Log.e(LOG, "ERROR:editBootomView方法要在addBootomView方法前调用");
        }
        return linearLayoutss;
    }


    public Listener listener;

    public interface Listener {
        public abstract void clickTab1(View view, int tabIndex, String tabTest);

        public abstract void clickTab2(View view, int tabIndex, String tabTest);

        public abstract void clickTab3(View view, int tabIndex, String tabTest);

        public abstract void clickTab4(View view, int tabIndex, String tabTest);

        public abstract void clickTab5(View view, int tabIndex, String tabTest);
    }

    public void listenerClick(Listener listener) {
        this.listener = listener;
    }


}
